package android.textIt.frontend.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.textIt.R;
import android.textIt.backend.dao.UserDao;
import android.textIt.backend.model.User;
import android.textIt.util.TextItUtil;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class GetStartedActivity extends Activity {
  private static final int ACTIVITY_MAIN = 0;
  private static UserDao userDAO;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.layout_get_started);
    userDAO = new UserDao(this);

    checkRegistered();
    bindOnClickHandlers();
    userDAO = new UserDao(this);

    String regId = TextItUtil.getPreferenceByKey(this, TextItUtil.REGISTRATION_KEY);
    String regSaved = TextItUtil.getPreferenceByKey(this, TextItUtil.REGISTRATION_ID_SAVED_KEY);
    if (regId.equals("")) {
      TextItUtil.registerC2DM(this);
    }
      userDAO.updateContacts();

  }

  private void bindOnClickHandlers() {
    Button btnGetStarted = (Button) findViewById(R.id.btnGetStarted);

    btnGetStarted.setOnClickListener(new Button.OnClickListener() {
      @Override
      public void onClick(View arg0) {
        TextView txtName = (TextView) findViewById(R.id.txtOwnerName);
        String name = txtName.getText().toString().trim();
        if (name.length() == 0) {
          new AlertDialog.Builder(GetStartedActivity.this).setTitle("Error")
                                                          .setMessage("Please enter a valid name")
                                                          .setNeutralButton("Ok",
                                                                            new DialogInterface.OnClickListener() {
                                                                              public void onClick(DialogInterface dlg,
                                                                                                  int sumthin) {

                                                                              }
                                                                            })
                                                          .show();
          return;
        }
        TextItUtil.registerUser(GetStartedActivity.this, name);
        String regId = TextItUtil.getPreferenceByKey(GetStartedActivity.this, TextItUtil.REGISTRATION_KEY);
        String regSaved = TextItUtil.getPreferenceByKey(GetStartedActivity.this, TextItUtil.REGISTRATION_ID_SAVED_KEY);
        if (regSaved.equals("") && !regId.equals("")){
        	userDAO.saveRegistrationId(regId);
        }
        checkRegistered();
      }
    });
  }

  private void checkRegistered() {
    UserDao userDAO = new UserDao(this);
    User u = userDAO.getUserbyID(1);
    if (u == null) { // user 1 is always the phone owner; if null, return so you can register
      return;
    }
    else { // user already registered, so start main activity
      Intent intent = new Intent();
      intent.setAction("TextIt.Main");
      startActivityForResult(intent, ACTIVITY_MAIN);
      return;
    }
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent extras) {
    Intent result = new Intent();
    setResult(resultCode, result);
    finish();
    return;
  }

}
